#include "LocalInfo.h"

#include "tinyxml.h"

#include "xyt.h"

LocalInfo::LocalInfo(void)
{
}

LocalInfo::LocalInfo(const std::string &xmlUrl)
{
	TiXmlDocument xmlDoc(xmlUrl.c_str());
	if (xmlDoc.LoadFile())
	{
		TiXmlNode* updateNode = xmlDoc.FirstChild("info");
		assert(updateNode);
		TiXmlElement* version = updateNode->FirstChildElement("version");
		assert(version);
		TiXmlElement* updateUrl = version->NextSiblingElement("updateUrl");
		assert(updateUrl);
		TiXmlElement* zipUrl = version->NextSiblingElement("localZipUrl");
		assert(zipUrl);

		m_version.setVersion(version->GetText());

		m_serverConfigXmlUrl = updateUrl->GetText();

		m_localZipUrl = zipUrl->GetText();
	}
}

LocalInfo::~LocalInfo(void)
{
}



bool LocalInfo::updateFile( const std::string &xmlUrl, const std::string &versionStr )
{
	TiXmlDocument xmlDoc;
	if (xmlDoc.LoadFile(xmlUrl.c_str()))
	{
		TiXmlNode* infoNode = xmlDoc.FirstChild("info");
		assert(infoNode);
		TiXmlElement* version = infoNode->FirstChildElement("version");
		assert(version);
		version->Clear();
		TiXmlText* text = new TiXmlText(versionStr.c_str());
		version->LinkEndChild(text);
		DeleteFileA(xmlUrl.c_str());
		if (xmlDoc.SaveFile(xmlUrl.c_str()))
		{
			return true;
		}else{
			//MsgBox::show("NO");
		}
		
	}
	return false;
}


void LocalInfo::setVersion( const Version &version )
{
	m_version = version;
}

Version LocalInfo::getVersion()
{
	return m_version;
}

void LocalInfo::setServerConfigXmlUrl( const std::string &fileUrl )
{
	m_serverConfigXmlUrl = fileUrl;
}

std::string LocalInfo::getServerConfigXmlUrl() const
{
	return m_serverConfigXmlUrl;
}

void LocalInfo::setLocalZipUrl( const std::string &zipUrl )
{
	m_localZipUrl = zipUrl;
}

std::string LocalInfo::getLocalZipUrl() const
{
	return m_localZipUrl;
}
